package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.CompanyMapper;
import com.jieshuibao.mapper.QuestionMapper;
import com.jieshuibao.mapper.QuestionUserMapper;
import com.jieshuibao.mapper.RedEnvelopesMapper;
import com.jieshuibao.mapper.RewardFlowJsbMapper;
import com.jieshuibao.mapper.SecretlyMapper;
import com.jieshuibao.mapper.UserDealRecordExMapper;
import com.jieshuibao.mapper.UserDealRecordMapper;
import com.jieshuibao.mapper.UserDramaMapper;
import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.mapper.WishbuyMapper;
import com.jieshuibao.mapper.WishcodeMapper;
import com.jieshuibao.model.db.Company;
import com.jieshuibao.model.db.Question;
import com.jieshuibao.model.db.QuestionUserKey;
import com.jieshuibao.model.db.RedEnvelopes;
import com.jieshuibao.model.db.Secretly;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.UserDealRecord;
import com.jieshuibao.model.db.UserDrama;
import com.jieshuibao.model.db.Wishbuy;
import com.jieshuibao.model.db.Wishcode;
import com.jieshuibao.util.MoneyRandom;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.UUIDGenerator;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class PayVipBanlanceService {
	@Resource
	private UserMapper userMapper;
	@Resource
	private CompanyMapper companyMapper;
	@Resource
	private RedEnvelopesMapper redEnvelopesMapper;
	@Resource
	private QuestionUserMapper questionUserMapper;
	@Resource
	private SecretlyMapper secretlyMapper;
	@Resource
	private RewardFlowJsbMapper rewardFlowJsbMapper;
	@Resource
	private QuestionMapper questionMapper;
	@Resource
	private UserDealRecordMapper userDealRecordMapper; 
	@Resource
	private UserDealRecordExMapper userDealRecordExMapper;
	@Resource
	private WishcodeMapper wishcodeMapper;
	@Resource
	private WishbuyMapper wishbuyMapper;
	
	@Resource
	private RedisService redisService;
	
	@Resource
	private UserDramaMapper userDramaMapper;

	/*
	 * 悬赏提交
	 */
	public int updateUserAndCompany(User user, double money,Question question) {
		try {
			if (user != null&&question!=null) {

				Integer companyRole = user.getCompanyRole();
				String uuid1 = UUIDGenerator.getUUID();
				String uuid2 = UUIDGenerator.getUUID();
				UserDealRecord userDealRecord1=new UserDealRecord();
				UserDealRecord userDealRecord2=new UserDealRecord();
				userDealRecord1.setUuid(uuid1);//用户的支出（会员卡 或者 企业会员卡）
				userDealRecord2.setUuid(uuid2);//解税宝公司收入
				
				userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
				userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
				userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
				userDealRecord1.setBusinessNo(question.getUuid());
				userDealRecord2.setBusinessNo(question.getUuid());
				Date date = new Date();
				userDealRecord1.setCreateDate(date);
				userDealRecord2.setCreateDate(date);
				userDealRecord1.setSum(money);
				userDealRecord2.setSum(money);
				userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_REWARD);
				userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_REWARD);
				userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
				userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
				userDealRecord1.setPaySucTime(date);
				userDealRecord2.setPaySucTime(date);
			
				userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				

				if (companyRole != 0) {

					// 企业用户
					String companyUuid = user.getCompanyUuid();
					if (companyUuid != null) {
						Company company = companyMapper
								.selectByPrimaryKey(companyUuid);
						if (company != null) {
							Double businessBalance = company
									.getBusinessBalance();
							if (money <= businessBalance) {
								company.setBusinessBalance(businessBalance
										- money);
								companyMapper
										.updateByPrimaryKeySelective(company);
								question.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								question.setPayStatus(PayCommonUtil.PAYSUCCESS_STATUS);
								questionMapper.insertSelective(question);
								userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								userDealRecord1.setUserId(companyUuid);
								userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								userDealRecordMapper.insertSelective(userDealRecord1);
								userDealRecordMapper.insertSelective(userDealRecord2);
								// 成功
								return 1;// 企业
							} else {
								// 余额不足
								return 4;
							}
						} else {
							//
							return 3;
						}
					} else {
						return 3;

					}

				} else {
					// 不是企业用户
					Double vipBalance = user.getVipBalance();
					if (money <= vipBalance) {
						User user3 = new User();
						user3.setPhoneNumber(user.getPhoneNumber());
						user3.setVipBalance(vipBalance - money);
						userMapper.updateByPrimaryKeySelective(user3);
						question.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						question.setPayStatus(PayCommonUtil.PAYSUCCESS_STATUS);
						questionMapper.insertSelective(question);
						userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						userDealRecord1.setUserId(user.getPhoneNumber());
						userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						userDealRecordMapper.insertSelective(userDealRecord1);
						userDealRecordMapper.insertSelective(userDealRecord2);
						return 2;// 普通
					} else {
						// 余额不足
						return 4;
					}
				}

			} else {
				return 3;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}

	// 用户发红包
	public int updateUserAndCompanyRedEnvelopes(User user, User user1,
			RedEnvelopes redEnvelopes, double money) {
		try {
			if (user != null && user1 != null && redEnvelopes != null) {
				User user2 = new User();
				Integer companyRole = user.getCompanyRole();

				String phoneNumber = user1.getPhoneNumber();
				Double balance = user1.getBalance();
				user2.setPhoneNumber(phoneNumber);
				user2.setBalance(balance + money);
				Date date = new Date();
				String uuid=UUIDGenerator.getUUID();
				//红包流水 发送者
				UserDealRecord userDealRecord = new UserDealRecord();
				userDealRecord.setUuid(uuid);
				userDealRecord.setBusinessNo(redEnvelopes.getUuid());
				
				userDealRecord.setCreateDate(date);
				userDealRecord.setSum(redEnvelopes.getMoney());
				userDealRecord.setPaySucTime(date);
				userDealRecord.setGoodsType(PayTypeUtil.GOODSTYPE_ENVELOPES);
				userDealRecord.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
				userDealRecord.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				//接受者
				String uuid2=UUIDGenerator.getUUID();
				UserDealRecord userDealRecord2 = new UserDealRecord();
				userDealRecord2.setUuid(uuid2);
				userDealRecord2.setUserId(user1.getPhoneNumber());
				
				userDealRecord2.setBusinessNo(redEnvelopes.getUuid());
				userDealRecord2.setCreateDate(date);
				userDealRecord2.setSum(redEnvelopes.getMoney());
				userDealRecord2.setPaySucTime(date);
				userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_ENVELOPES);
				userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
				userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				if (companyRole != 0) {

					// 企业用户
					String companyUuid = user.getCompanyUuid();
					if (companyUuid != null) {
						Company company = companyMapper
								.selectByPrimaryKey(companyUuid);
						if (company != null) {
							Double businessBalance = company
									.getBusinessBalance();
							if (money <= businessBalance) {
								company.setBusinessBalance(businessBalance
										- money);
								companyMapper
										.updateByPrimaryKeySelective(company);

								userMapper.updateByPrimaryKeySelective(user2);
								redEnvelopes.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								redEnvelopesMapper
										.insertSelective(redEnvelopes);
								userDealRecord.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								userDealRecord.setUserId(companyUuid);
								userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								userDealRecordMapper.insertSelective(userDealRecord);
								userDealRecordMapper.insertSelective(userDealRecord2);
								// 成功
								return 1;// 企业
							} else {
								// 余额不足
								return 4;
							}
						} else {
							//
							return 3;
						}
					} else {
						return 3;

					}

				} else {
					// 不是企业用户
					Double vipBalance = user.getVipBalance();
					if (money <= vipBalance) {
						User user3 = new User();
						user3.setPhoneNumber(user.getPhoneNumber());
						user3.setVipBalance(vipBalance - money);
						userMapper.updateByPrimaryKeySelective(user3);

						userMapper.updateByPrimaryKeySelective(user2);
						redEnvelopes.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						redEnvelopesMapper.insertSelective(redEnvelopes);
						userDealRecord.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						userDealRecord.setUserId(user.getPhoneNumber());
						userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						userDealRecordMapper.insertSelective(userDealRecord);
						userDealRecordMapper.insertSelective(userDealRecord2);
						return 2;// 普通
					} else {
						// 余额不足
						return 4;
					}
				}

			} else {
				return 3;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}

	// 偷偷看
	/**
	 * 
	 * @param user 偷看者
	 * @param user1 公开问题者
	 * @param user4 回答者
	 * @param questionUserKey1
	 * @param questionUserKey2
	 * @param money
	 * @param secretly
	 * @param userDealRecords
	 * @return
	 */
	public int updateUserAndCompanyQuestionUserKey(User user, User user1,User user4,
			QuestionUserKey questionUserKey1, QuestionUserKey questionUserKey2,
			double money, Secretly secretly,List<UserDealRecord> userDealRecords) {
		try {
			if (user != null && user1 != null&&user4!=null) {
				
				
				
				Integer companyRole = user.getCompanyRole();
				double subMoney = MoneyRandom.getSubMoney(money);
				double subMoneyFifteen = MoneyRandom.getSubMoneyFifteen(money);
				//String phoneNumber = user1.getPhoneNumber();
				//Double balance = user1.getBalance();
				//user2.setPhoneNumber(phoneNumber);
				//user2.setBalance(balance + subMoney);
				
				Date date = new Date();
				if (companyRole != 0) {

					// 企业用户
					String companyUuid = user.getCompanyUuid();
					if (companyUuid != null) {
						Company company = companyMapper
								.selectByPrimaryKey(companyUuid);
						if (company != null) {
							Double businessBalance = company
									.getBusinessBalance();
							if (money <= businessBalance) {
								company.setBusinessBalance(businessBalance
										- money);
								companyMapper
										.updateByPrimaryKeySelective(company);
								if(user1!=null){
									User user2 = new User();
									String phoneNumber = user1.getPhoneNumber();
									Double balance = user1.getBalance();
									user2.setPhoneNumber(phoneNumber);
									user2.setBalance(balance + subMoney);
									userMapper.updateByPrimaryKeySelective(user2);
								}
								if(user4!=null){
									User user5 = new User();
									user5.setPhoneNumber(user4.getPhoneNumber());
									user5.setBalance(user4.getBalance()+subMoneyFifteen);
									userMapper.updateByPrimaryKeySelective(user5);
								}
								// 记录偷看（用户 和数量）
								if (questionUserKey1 == null) {
									questionUserMapper
											.insertSelective(questionUserKey2);
								}
								if (secretly != null) {
									secretly.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
									secretlyMapper.insertSelective(secretly);
								}
								if(userDealRecords!=null){
									for (UserDealRecord userDealRecord : userDealRecords) {
										userDealRecord.setPaySucTime(date);
										userDealRecord.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
										userDealRecord.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
										userDealRecordMapper.insertSelective(userDealRecord);
									}
									
								}
								// 成功
								return 1;// 企业
							} else {
								// 余额不足
								return 4;
							}
						} else {
							//
							return 3;
						}
					} else {
						return 3;

					}

				} else {/*
					// 不是企业用户
					Double vipBalance = user.getVipBalance();
					if (money <= vipBalance) {
						User user3 = new User();
						user3.setPhoneNumber(user.getPhoneNumber());
						user3.setVipBalance(vipBalance - money);
						userMapper.updateByPrimaryKeySelective(user3);

						userMapper.updateByPrimaryKeySelective(user2);
						userMapper.updateByPrimaryKeySelective(user5);
						// 记录偷看（用户 和数量）
						if (questionUserKey1 == null) {
							questionUserMapper
									.insertSelective(questionUserKey2);
						}
						if (secretly != null) {
							secretly.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
							secretlyMapper.insertSelective(secretly);
						}
						if(userDealRecords!=null){
							for (UserDealRecord userDealRecord : userDealRecords) {
								userDealRecord.setPaySucTime(date);
								userDealRecord.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
								userDealRecord.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
								userDealRecordMapper.insertSelective(userDealRecord);
							}
							
						}
						return 2;// 普通
					} else {
						// 余额不足
						return 4;
					}
				*/
					}
				return 3;
			} else {
				return 3;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}
	//偷偷看 修改
	public int updateTwoUserAndCompanyQuestionUserKey(User user, User user1,User user4,
			QuestionUserKey questionUserKey1, QuestionUserKey questionUserKey2,
			double money, Secretly secretly,List<UserDealRecord> userDealRecords) {
		try {
			if (user != null && user1 != null&&user4!=null) {
				User user2 = new User();
				User user5 = new User();
				
				Integer companyRole = user.getCompanyRole();
				double subMoney = MoneyRandom.getSubMoney(money);
				double subMoneyFifteen = MoneyRandom.getSubMoneyFifteen(money);
				String phoneNumber = user1.getPhoneNumber();
				Double balance = user1.getBalance();
				user2.setPhoneNumber(phoneNumber);
				user2.setBalance(balance + subMoney);
				user5.setPhoneNumber(user4.getPhoneNumber());
				user5.setBalance(user4.getBalance()+subMoneyFifteen);
				Date date = new Date();
				if (companyRole != 0) {

					// 企业用户
					String companyUuid = user.getCompanyUuid();
					if (companyUuid != null) {
						Company company = companyMapper
								.selectByPrimaryKey(companyUuid);
						if (company != null) {
							Double businessBalance = company
									.getBusinessBalance();
							if (money <= businessBalance) {
								company.setBusinessBalance(businessBalance
										- money);
								companyMapper
										.updateByPrimaryKeySelective(company);

								userMapper.updateByPrimaryKeySelective(user2);
								userMapper.updateByPrimaryKeySelective(user5);
								// 记录偷看（用户 和数量）
								if (questionUserKey1 == null) {
									questionUserMapper
											.insertSelective(questionUserKey2);
								}
								if (secretly != null) {
									secretly.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
									secretlyMapper.insertSelective(secretly);
								}
								if(userDealRecords!=null){
									for (UserDealRecord userDealRecord : userDealRecords) {
										userDealRecord.setPaySucTime(date);
										userDealRecord.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
										userDealRecord.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
										userDealRecordMapper.insertSelective(userDealRecord);
									}
									
								}
								// 成功
								return 1;// 企业
							} else {
								// 余额不足
								return 4;
							}
						} else {
							//
							return 3;
						}
					} else {
						return 3;

					}

				} else {
					//不是企业用户
					return 3;
				}

			} else {
				return 3;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}
	//许愿支付
	public int updateUserAndAddWishBuy(User user,Wishbuy wishbuy) {
		try {
			if (user != null && wishbuy != null) {
				
				Integer companyRole = user.getCompanyRole();
				int money = wishbuy.getMoney();
				String id = wishbuy.getId();
				String wishId = wishbuy.getWishId();
				
				int buyCount = wishbuy.getBuyCount();
				String phoneNumber = user.getPhoneNumber();
				
				Date date = new Date();
				
				//流水 
				UserDealRecord userDealRecord1 = new UserDealRecord();
				UserDealRecord userDealRecord2 = new UserDealRecord();
				String uuid1 = UUIDGenerator.getUUID();//用户支出流水 或者企业支出
				String uuid2 = UUIDGenerator.getUUID();//公司收入流水
				//userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
				userDealRecord1.setBusinessNo(id);
				userDealRecord1.setCreateDate(date);
				userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_WISHS);
				userDealRecord1.setPaySucTime(date);
				//userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecord1.setSum((double)money);
				userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
				//userDealRecord1.setUserId(phoneNumber);
				userDealRecord1.setUuid(uuid1);
				
				userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
				userDealRecord2.setBusinessNo(id);
				userDealRecord2.setCreateDate(date);
				userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_WISHS);
				userDealRecord2.setPaySucTime(date);
				//userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecord2.setSum((double)money);
				userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
				userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
				userDealRecord2.setUuid(uuid2);
				if (companyRole != 0) {

					// 企业用户
					String companyUuid = user.getCompanyUuid();
					if (companyUuid != null) {
						Company company = companyMapper
								.selectByPrimaryKey(companyUuid);
						if (company != null) {
							Double businessBalance = company
									.getBusinessBalance();
							if (money <= businessBalance) {
								company.setBusinessBalance(businessBalance
										- money);
								companyMapper.updateByPrimaryKeySelective(company);
								userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);		
								userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								userDealRecord1.setUserId(companyUuid);
								userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								userDealRecordMapper.insertSelective(userDealRecord1);
								userDealRecordMapper.insertSelective(userDealRecord2);
								for (int i = 0; i <buyCount; i++) {
									Wishcode wishcode = new Wishcode();
									String wishcodeid = UUIDGenerator.getUUID();
									wishcode.setId(wishcodeid);
									wishcode.setBuywithId(id);
									wishcode.setUserId(phoneNumber);
									Long incrby = redisService.getIncrby(wishId+"_code");
									String valueOf = String.valueOf(incrby);
									wishcode.setWishId(wishId);
									wishcode.setCode(valueOf);
									wishbuy.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
									wishbuy.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
									wishcodeMapper.insertSelective(wishcode);
								}
								wishbuyMapper.insertSelective(wishbuy);
								// 成功
								return 1;// 企业
							} else {
								// 余额不足
								return 4;
							}
						} else {
							//
							return 3;
						}
					} else {
						return 3;

					}

				} else {
					// 不是企业用户
					Double vipBalance = user.getVipBalance();
					if (money <= vipBalance) {
						User user2= new User();
						user2.setPhoneNumber(phoneNumber);
						user2.setVipBalance(vipBalance - money);
						userMapper.updateByPrimaryKeySelective(user2);
						userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);		
						userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						userDealRecord1.setUserId(phoneNumber);
						userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						userDealRecordMapper.insertSelective(userDealRecord1);
						userDealRecordMapper.insertSelective(userDealRecord2);
						for (int i = 0; i <buyCount; i++) {
							Wishcode wishcode = new Wishcode();
							String wishcodeid = UUIDGenerator.getUUID();
							wishcode.setId(wishcodeid);
							wishcode.setBuywithId(id);
							wishcode.setUserId(phoneNumber);
							Long incrby = redisService.getIncrby(wishId+"_code");
							String valueOf = String.valueOf(incrby);
							wishcode.setWishId(wishId);
							wishcode.setCode(valueOf);
							wishbuy.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
							wishbuy.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
							wishcodeMapper.insertSelective(wishcode);
						}
						wishbuyMapper.insertSelective(wishbuy);
						
						return 2;// 普通
					} else {
						// 余额不足
						return 4;
					}
				}

			} else {
				return 3;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}
	//购买视频
	public int updateUserAndAddUserDrama(User user,UserDrama userDrama) {
		try {
			if (user != null && userDrama != null) {
				
				Integer companyRole = user.getCompanyRole();
				Double money = userDrama.getMoney();
				
				String id = userDrama.getId();
				
				String phoneNumber = user.getPhoneNumber();
				
				Date date = new Date();
				
				//流水 
				UserDealRecord userDealRecord1 = new UserDealRecord();
				UserDealRecord userDealRecord2 = new UserDealRecord();
				String uuid1 = UUIDGenerator.getUUID();//用户支出流水 或者企业支出
				String uuid2 = UUIDGenerator.getUUID();//解税宝收入流水
				//userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);
				userDealRecord1.setBusinessNo(id);
				userDealRecord1.setCreateDate(date);
				userDealRecord1.setGoodsType(PayTypeUtil.GOODSTYPE_VIDEOS);
				userDealRecord1.setPaySucTime(date);
				//userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				userDealRecord1.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecord1.setSum(money);
				userDealRecord1.setTradeType(PayTypeUtil.TRDETYPE_EXPENDITURE);
				//userDealRecord1.setUserId(phoneNumber);
				userDealRecord1.setUuid(uuid1);
				
				userDealRecord2.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);
				userDealRecord2.setBusinessNo(id);
				userDealRecord2.setCreateDate(date);
				userDealRecord2.setGoodsType(PayTypeUtil.GOODSTYPE_VIDEOS);
				userDealRecord2.setPaySucTime(date);
				//userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				userDealRecord2.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				userDealRecord2.setSum(money);
				userDealRecord2.setTradeType(PayTypeUtil.TRDETYPE_INCOME);
				userDealRecord2.setUserId(PayTypeUtil.COMPANY_ACCPUNT);
				userDealRecord2.setUuid(uuid2);
				if (companyRole != 0) {

					// 企业用户
					String companyUuid = user.getCompanyUuid();
					if (companyUuid != null) {
						Company company = companyMapper
								.selectByPrimaryKey(companyUuid);
						if (company != null) {
							Double businessBalance = company
									.getBusinessBalance();
							if (money <= businessBalance) {
								company.setBusinessBalance(businessBalance
										- money);
								companyMapper.updateByPrimaryKeySelective(company);
								userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_BUSINESS);		
								userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								userDealRecord1.setUserId(companyUuid);
								userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_COMPANYBALANCE);
								userDealRecordMapper.insertSelective(userDealRecord1);
								userDealRecordMapper.insertSelective(userDealRecord2);
								userDramaMapper.insertSelective(userDrama);
								
								// 成功
								return 1;// 企业
							} else {
								// 余额不足
								return 4;
							}
						} else {
							//
							return 3;
						}
					} else {
						return 3;

					}

				} else {
					// 不是企业用户
					Double vipBalance = user.getVipBalance();
					if (money <= vipBalance) {
						User user2= new User();
						user2.setPhoneNumber(phoneNumber);
						user2.setVipBalance(vipBalance - money);
						userMapper.updateByPrimaryKeySelective(user2);
						userDealRecord1.setAccountType(PayTypeUtil.ACCOUNTTYPE_PERSONAL);		
						userDealRecord1.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						userDealRecord1.setUserId(phoneNumber);
						userDealRecord2.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
						userDealRecordMapper.insertSelective(userDealRecord1);
						userDealRecordMapper.insertSelective(userDealRecord2);
						userDramaMapper.insertSelective(userDrama);
						return 2;// 普通
					} else {
						// 余额不足
						return 4;
					}
				}

			} else {
				return 3;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

	}
	
}
